package com.itheima.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.itheima.entity.AccountMessage;
import com.itheima.entity.Preject;
import com.itheima.entity.ProjectReview;
import com.itheima.entity.ProjectReviewProgress;
import com.itheima.mapper.AccountMessageMapper;
import com.itheima.mapper.PrejectMapper;
import com.itheima.mapper.ProjectReviewMapper;
import com.itheima.mapper.ProjectReviewProgressMapper;
import com.itheima.pojo.po.ProjectReviewPo;
import com.itheima.pojo.po.ProjectReviewProgressPo;
import com.itheima.service.IProjectReviewProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目审核进度Service业务层处理
 *
 * @author guanhuaer
 * @date 2025-03-18
 */
@Service
public class ProjectReviewProgressServiceImpl implements IProjectReviewProgressService {
    @Autowired
    private ProjectReviewProgressMapper projectReviewProgressMapper;
    @Autowired
    private ProjectReviewMapper reviewMapper;
    @Autowired
    private AccountMessageMapper messageMapper;
    @Autowired
    private PrejectMapper prejectMapper;

    /**
     * 查询项目审核进度
     *
     * @param id 项目审核进度主键
     * @return 项目审核进度
     */
    @Override
    public ProjectReviewProgress selectProjectReviewProgressById(Long id) {
        return projectReviewProgressMapper.selectProjectReviewProgressById(id);
    }

    /**
     * 查询项目审核进度列表
     *
     * @param projectReviewProgress 项目审核进度
     * @return 项目审核进度
     */
    @Override
    public List<ProjectReviewProgress> selectProjectReviewProgressList(ProjectReviewProgress projectReviewProgress) {
        return projectReviewProgressMapper.selectProjectReviewProgressList(projectReviewProgress);
    }

    /**
     * 新增项目审核进度
     *
     * @param projectReviewProgress 项目审核进度
     * @return 结果
     */
    @Override
    public int insertProjectReviewProgress(ProjectReviewProgress projectReviewProgress) {
        return projectReviewProgressMapper.insertProjectReviewProgress(projectReviewProgress);
    }

    /**
     * 修改项目审核进度
     *
     * @param projectReviewProgress 项目审核进度
     * @return 结果
     */
    @Override
    public int updateProjectReviewProgress(ProjectReviewProgress projectReviewProgress) {
        return projectReviewProgressMapper.updateProjectReviewProgress(projectReviewProgress);
    }


    @Override
    public ProjectReviewPo selectProjectList(Long projectId) {
        ProjectReviewPo reviewPo = new ProjectReviewPo();
        reviewPo.setReviewList(reviewMapper.selectReviewList(projectId));
        reviewPo.setProgressList(projectReviewProgressMapper.selectProgressList(projectId));
        return reviewPo;
    }

    //新增项目审核进度
    @Override
    @Transactional
    public int insertProjectReview(ProjectReview projectReview) {
        //新增学生消息
        AccountMessage message = new AccountMessage();
        message.setUserId(projectReview.getUserId());
        message.setTitle("项目提交审核申请");
        message.setDescription("您提交的项目《" + projectReview.getProjectName() + "》已提交审核，请等待审核结果。");
        message.setDatetime(new Date());
        message.setType(1L);
        message.setIsRead(0L);
        messageMapper.insertAccountMessage(message);
        //新增教师消息
        message.setUserId(projectReview.getTeacherId());
        message.setTitle("项目审核申请");
        message.setDescription(projectReview.getPerson() + "创建的项目" + projectReview.getProjectName() + "提交了审核申请");
        message.setDatetime(new Date());
        message.setType(2L);
        message.setExtra("未开始");
        message.setStatus("info");
        message.setIsRead(0L);
        messageMapper.insertAccountMessage(message);
        //新增项目审核进度
        // 根据项目id查询项目是否已经存在审核进度
        ProjectReview progress = reviewMapper.selectReviewList(projectReview.getProjectId());
        if (progress == null) {
            projectReview.setSTime(LocalDateTime.now());
            projectReview.setCreateTime(LocalDateTime.now());
            projectReview.setActive(1);
            return reviewMapper.insertProjectReview(projectReview);
        }
        // 已存在审核进度，更新审核进度
        // 1. 更新项目审核进度
        progress.setSTime(LocalDateTime.now());
        progress.setCreateTime(LocalDateTime.now());
        progress.setActive(1);
        return reviewMapper.updateProjectReview(progress);
    }

    //审核项目
    @Override
    @Transactional
    public void projectAudit(ProjectReviewProgressPo progressPo) {
        // 1. 更新项目审核进度
        ProjectReview review = new ProjectReview();
        // 2. 更新项目审核过程
        ProjectReviewProgress progress = new ProjectReviewProgress();
        // 3. 推送消息
        AccountMessage message = new AccountMessage();
        if (progressPo.getProjectIds() != null) {
            for (Long projectId : progressPo.getProjectIds()) {
                // 3. 更新项目审核进度
                review.setProjectId(projectId);
                review.setPerson(progressPo.getPerson());
                review.setAuditResult(progressPo.getAuditResult());
                if (progressPo.getRole().equals("教师")) {
                    review.setTTime(LocalDateTime.now());
                    review.setReviewer(progressPo.getReviewer());
                    if (progressPo.getAuditResult() == 0) {
                        review.setActive(2);
                    }
                } else {
                    if (progressPo.getAuditResult() == 0) {
                        review.setActive(3);
                    }
                    review.setModifiedBy(progressPo.getReviewer());
                    review.setATime(LocalDateTime.now());
                }
                review.setReviewComments(progressPo.getReviewComments());
                review.setReviewDate(LocalDateTime.now());
                review.setUpdateTime(new Date());
                reviewMapper.updateProjectAuditReview(review);
                // 4. 更新项目审核过程
                progress.setProjectId(projectId);
                progress.setReviewer(progressPo.getReviewer());
                progress.setRole(progressPo.getRole());
                progress.setReviewComments(progressPo.getReviewComments());
                progress.setAuditResult(progressPo.getAuditResult());
                projectReviewProgressMapper.insertProjectReviewProgress(progress);
                // 5. 推送学生消息
                message.setUserId(progressPo.getUserId());
                message.setTitle(progressPo.getRole() + " 审核 "+progressPo.getProjectName() + (progressPo.getAuditResult() == 0 ? "通过" : progressPo.getAuditResult() == 1 ? "不通过" : "驳回"));
                message.setDescription(progressPo.getRole()+progressPo.getReviewComments());
                message.setDatetime(new Date());
                message.setType(0L);
                message.setIsRead(0L);
                messageMapper.insertAccountMessage(message);
                // 6. 推送教师消息
                message.setUserId(progressPo.getReviewerId());
                message.setTitle(progressPo.getProjectName() + "  已审核 " + (progressPo.getAuditResult() == 0 ? "通过" : progressPo.getAuditResult() == 1 ? "不通过" : "驳回"));
                message.setDescription(progressPo.getReviewComments());
                message.setDatetime(new Date());
                message.setType(2L);
                message.setExtra("已完成");
                message.setStatus("success");
                message.setIsRead(0L);
                messageMapper.insertAccountMessage(message);

                // 7. 更新项目状态
                Preject preject = new Preject();
                preject.setId(progressPo.getId());
                Integer result = progressPo.getAuditResult();
                preject.setStatus(result == 0 ? 1 : result == 1 ? 2 : 3);
                // 审核不通过，或者驳回，将项目置为未提交状态
//                if (progressPo.getAuditResult() == 1 || progressPo.getAuditResult() == 2) {
//                    preject.setIsSubmit(0);
//                }
                prejectMapper.projectEdit(preject);
            }
        }
    }

    @Override
    public List<ProjectReview> selectProjectIdsList(Integer[] projectIds) {
        return reviewMapper.selectProjectIdsList(projectIds);
    }

    @Override
    public List<Integer> selectIdsByUsername(String username) {
        return reviewMapper.selectIdsByUsername(username);
    }

}
